home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / extra_2 / flmgmtcl.zip / DIRLIST.CPP < prev    next >
C/C++ Source or Header  |  1995-11-02  |  6KB  |  214 lines

  1. // ==========================================================================
  2. //                             Class Implementation : CDirList
  3. // ==========================================================================
  4.  
  5. // Source file : dirlist.cpp
  6.  
  7. // Source : Periphere NV (R.Mortelmans)
  8. // Creation Date :        2nd November 1995
  9. // Last Modification : 2nd November 1995
  10.                           
  11. // //////////////////////////////////////////////////////////////////////////
  12.  
  13. #include "stdafx.h"        // standard MFC include
  14. #include "dirlist.h"    // class specification
  15. #include "dos.h"        // for _dos_findfirst, ...
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. IMPLEMENT_DYNAMIC(CDirList, CObject)
  23.  
  24. #define new DEBUG_NEW
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // Definition of static members
  28.  
  29.  
  30. // Data members -------------------------------------------------------------
  31. // protected:
  32.     // CPath m_path;
  33.     // --- The path specification where to look for Dirs
  34.     
  35.     // CObList m_dirList;            // Contains objects of class CDirSpec
  36.     // --- The list of dir specifications found in the above specified path
  37.  
  38. // private:
  39.     
  40. // Member functions ---------------------------------------------------------
  41. // public:
  42.  
  43. CDirList::CDirList()
  44.     :
  45.     m_path()
  46.     {
  47.     }
  48.     
  49. CPathSpec CDirList::GetPath() const
  50.     {
  51.     return m_path;
  52.     }
  53.     
  54. BOOL CDirList::SetPath(CPathSpec path)
  55.     {
  56.     if (path.GetFileName().IsEmpty())
  57.         path.SetFileName(TEXT("*.*"));
  58.     if (path.MakeAbsolute())
  59.         {
  60.         m_path = path;
  61.         return TRUE;
  62.         }
  63.     else
  64.         {
  65.         TRACE(TEXT("CDirList::SetPath : Path spec is invalid : %s\n"), path.GetPath());
  66.         return FALSE;
  67.         }
  68.     }   
  69.     
  70. BOOL CDirList::Search()
  71.     {
  72. #ifdef WIN32
  73.     CDirSpec* pDir;
  74.     WIN32_FIND_DATA fileData;
  75.     HANDLE hFindFile;
  76.     BOOL bFileFound(TRUE);
  77.         
  78.     hFindFile = FindFirstFile(m_path.GetPath(), &fileData);
  79.     if(hFindFile != INVALID_HANDLE_VALUE)
  80.         {
  81.         while (bFileFound)
  82.             {
  83.             if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
  84.                 (fileData.cFileName[0] != __TEXT('.')) && 
  85.                 (fileData.cFileName[0] != __TEXT('..')))
  86.                 {
  87.                 pDir = new CDirSpec(m_path.GetDirectory());
  88.                 pDir->AppendDirectory(CDirSpec(fileData.cFileName));
  89.                 m_dirArray.Add(pDir);
  90.                 }
  91.  
  92.             bFileFound = FindNextFile(hFindFile, &fileData);
  93.             }
  94.         FindClose(hFindFile);
  95.         }
  96.     return TRUE;
  97.  
  98. #else
  99.     CDirSpec* pDir;
  100.     _find_t fileInfo;
  101.     BOOL bFileFound;
  102.     
  103.     bFileFound = (_dos_findfirst(m_path.GetPath(), _A_NORMAL | _A_ARCH | _A_SUBDIR, &fileInfo) == 0);
  104.     while (bFileFound)
  105.         {
  106.         if ((fileInfo.attrib & _A_SUBDIR) && (strcmp(fileInfo.name, TEXT(".")) != 0) &&
  107.             (strcmp(fileInfo.name, TEXT("..")) != 0))
  108.             {
  109.             pDir = new CDirSpec(m_path.GetDirectory());
  110.             pDir->AppendDirectory(CDirSpec(fileInfo.name));
  111.             m_dirArray.Add(pDir);
  112.             }
  113.  
  114.         bFileFound = (_dos_findnext(&fileInfo) == 0);
  115.         }        
  116.     return TRUE;
  117. #endif
  118.     }
  119.     
  120. const CObArray* CDirList::GetList() const
  121.     {
  122.     return &m_dirArray;
  123.     }
  124.  
  125. const CDirSpec* CDirList::GetAt(int nIndex) const
  126. {
  127.     return (CDirSpec*) m_dirArray.GetAt(nIndex);
  128. }
  129.     
  130. void CDirList::Sort()
  131.     {
  132.     // This algorithm will sort a list using selection sort
  133.     // The array contains objects from position 0 to END
  134.     // nIndexNotSorted points to the first not yet sorted object,
  135.     //  so the set [0..nIndexNotSorted - 1] is already sorted
  136.     // This algorithm searches the not yet sorted objects in the set
  137.     //  [nIndexNotSorted..END] for the smallest object and adds this 
  138.     //  to the sorted objects
  139.     // nIndexCompare point to the last object in this set that has 
  140.     //  already been examined
  141.     // The algorithm ends when all objects are sorted
  142.     int nIndexNotSorted = 0;
  143.     int nIndexCompare = 0;
  144.     CDirSpec* pSmallestDir = NULL;    // point to array[nIndexSorted]
  145.     CDirSpec* pCompareDir = NULL;        // point to array[nIndexCompare]
  146.     CDirSpec* pSwapDir;
  147.     
  148.     while (nIndexNotSorted <= m_dirArray.GetUpperBound())
  149.         {
  150.         ASSERT(m_dirArray[nIndexNotSorted]->IsKindOf(RUNTIME_CLASS(CDirSpec)));
  151.         pSmallestDir = (CDirSpec*)m_dirArray[nIndexNotSorted];
  152.         nIndexCompare = nIndexNotSorted;
  153.         nIndexCompare++;
  154.         
  155.         while (nIndexCompare <= m_dirArray.GetUpperBound()) 
  156.             {
  157.             ASSERT(m_dirArray[nIndexCompare]->IsKindOf(RUNTIME_CLASS(CDirSpec)));
  158.             pCompareDir = (CDirSpec*)m_dirArray[nIndexCompare];
  159.             if (*pCompareDir < *pSmallestDir)
  160.                 {
  161.                 pSwapDir = pSmallestDir;
  162.                 m_dirArray[nIndexNotSorted] = m_dirArray[nIndexCompare];
  163.                 m_dirArray[nIndexCompare] = pSwapDir;
  164.                 pSmallestDir = (CDirSpec*)m_dirArray[nIndexNotSorted];
  165.                 }
  166.             nIndexCompare++;
  167.             }
  168.         nIndexNotSorted++;
  169.         }
  170.     }
  171.     
  172. void CDirList::ClearList()
  173.     {
  174.     CDirSpec* pDir;
  175.     int nIndex = 0;
  176.     int nMaxIndex = m_dirArray.GetUpperBound();
  177.     while (nIndex <= nMaxIndex) 
  178.         {
  179.         ASSERT(m_dirArray[nIndex]->IsKindOf(RUNTIME_CLASS(CDirSpec)));
  180.         pDir = (CDirSpec*)m_dirArray[nIndex];
  181.         delete pDir;
  182.         nIndex++;
  183.         }
  184.     m_dirArray.RemoveAll();
  185.     }
  186.     
  187. #ifdef _DEBUG
  188. void CDirList::Dump(CDumpContext& dc) const
  189.     {
  190.     CObject::Dump(dc);
  191.     dc << TEXT("\nm_path : ");
  192.     m_path.Dump(dc);
  193.     dc << TEXT("\nm_dirArray : ") << m_dirArray;    
  194.     }
  195.  
  196. void CDirList::AssertValid() const
  197.     {
  198.     CObject::AssertValid();
  199.     }
  200. #endif
  201.  
  202. CDirList::~CDirList()
  203.     {
  204.     ClearList();
  205.     }
  206.     
  207. // protected:
  208.  
  209. // private:
  210.  
  211. // Message handlers ---------------------------------------------------------
  212.  
  213. // ==========================================================================
  214.